package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.biz.transfer.ITransferYwOrdernoBankService;
import com.zjdiepu.www.proxy.dao.transfer.TransferYwOrdernoBankMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferYwOrdernoBank;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferYwOrdernoBankExt;

/**
 * 中转_yw_银行订单号Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月18日 13时06分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferYwOrdernoBankServiceImpl extends BaseService implements ITransferYwOrdernoBankService {
	public static final Logger logger = LoggerFactory.getLogger(TransferYwOrdernoBankServiceImpl.class);

	@Autowired
	private TransferYwOrdernoBankMapper transferYwOrdernoBankMapper;
	
	@Override
	public List<TransferYwOrdernoBank> getByBusiId(String busiId) {
		TransferYwOrdernoBankExt transferYwOrdernoBankExt = new TransferYwOrdernoBankExt();
		transferYwOrdernoBankExt.setBusiId(busiId);
		transferYwOrdernoBankExt.setWhereSql(" AND (`ORDE_RNO` IS NOT NULL AND `ORDE_RNO` <> '')");
		transferYwOrdernoBankExt.setDpStatus(DpStatus.NORMAL.getCode());
		transferYwOrdernoBankExt.setOrderSql(" `CREATED` DESC");
//		transferYwOrdernoBankExt.setRows(1);
		List<TransferYwOrdernoBank> transferYwOrdernoBanks = transferYwOrdernoBankMapper.selectByExt(transferYwOrdernoBankExt);
		if(CollectionUtils.isEmpty(transferYwOrdernoBanks)) {
			return null;
		}
		return transferYwOrdernoBanks;
	}
	
	@Override
	public TransferYwOrdernoBank getByOrderNo(String orderNo) {
		TransferYwOrdernoBankExt transferYwOrdernoBankExt = new TransferYwOrdernoBankExt();
		transferYwOrdernoBankExt.setOrdeRno(orderNo);
		transferYwOrdernoBankExt.setWhereSql(" AND (`BUSI_ID` IS NOT NULL AND `BUSI_ID` <> '')");
		transferYwOrdernoBankExt.setDpStatus(DpStatus.NORMAL.getCode());
		transferYwOrdernoBankExt.setOrderSql(" `CREATED` DESC");
		transferYwOrdernoBankExt.setRows(1);
		List<TransferYwOrdernoBank> transferYwOrdernoBanks = transferYwOrdernoBankMapper.selectByExt(transferYwOrdernoBankExt);
		if(CollectionUtils.isEmpty(transferYwOrdernoBanks)) {
			return null;
		}
		return transferYwOrdernoBanks.get(0);
	}
	
	@Override
	public void saveUpdateAndDelete(TransferYwOrdernoBank transferYwOrdernoBank, OperateType operType) {
		switch (operType) {
		case add:
			transferYwOrdernoBank.initInsertData(transferYwOrdernoBank.getModifier(), transferYwOrdernoBank.getModifierName());
			transferYwOrdernoBankMapper.insert(transferYwOrdernoBank);
			break;
		case edit:
			transferYwOrdernoBank.initUpdateData(transferYwOrdernoBank.getModifier(), transferYwOrdernoBank.getModifierName());
			// 影响的记录数
			int updateCount = transferYwOrdernoBankMapper.updateByPrimaryKeySelective(transferYwOrdernoBank);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferYwOrdernoBank.initDeleteData(transferYwOrdernoBank.getModifier(), transferYwOrdernoBank.getModifierName());
			// 影响的记录数
			int deleteCount = transferYwOrdernoBankMapper.updateByPrimaryKeySelective(transferYwOrdernoBank);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferYwOrdernoBank> selectByExt(TransferYwOrdernoBankExt transferYwOrdernoBankExt) {
		return transferYwOrdernoBankMapper.selectByExt(transferYwOrdernoBankExt);
	}
	
	@Override
	public int countByExt(TransferYwOrdernoBankExt transferYwOrdernoBankExt){
		return transferYwOrdernoBankMapper.countByExt(transferYwOrdernoBankExt);
	}
	
	@Override
	public TransferYwOrdernoBank selectById(String id) {
		return transferYwOrdernoBankMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferYwOrdernoBankMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferYwOrdernoBank transferYwOrdernoBank) {
		return transferYwOrdernoBankMapper.countRefined(transferYwOrdernoBank);
	}

	@Override
	public TransferYwOrdernoBank selectRefined(TransferYwOrdernoBank transferYwOrdernoBank) {
		return transferYwOrdernoBankMapper.selectRefined(transferYwOrdernoBank);
	}
	
	@Override
	public int deletedRefined(TransferYwOrdernoBank transferYwOrdernoBank) {
		return transferYwOrdernoBankMapper.deletedRefined(transferYwOrdernoBank);
	}

	@Override
	public boolean repeat(TransferYwOrdernoBank transferYwOrdernoBank) {
		int count = countRefined(transferYwOrdernoBank);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferYwOrdernoBank.getId())) {
			return true;
		}
		TransferYwOrdernoBank dbItem = selectRefined(transferYwOrdernoBank);
		if(null == dbItem) {
			return false;
		}
		return !transferYwOrdernoBank.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferYwOrdernoBankMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferYwOrdernoBank> transferYwOrdernoBanks) {
		return transferYwOrdernoBankMapper.insertBatch(transferYwOrdernoBanks);
	}
}
